<?php
require ("config.php");

function browse ($database) {
	echo "Browsing Users:<br>";
	$db=mysql_connect($database['host'],$database['user'],$database['password']) or die("Could not connect");
	mysql_select_db($database['database']) or die('Could not select database');
	$sql="select * from ".$database['users_table'];
	$result=mysql_query($sql);
	echo "<center><table border=0>";
        echo "<tr bgcolor=#a9a9a9><th>Index</th><th>User</th></tr>";
        while($row = mysql_fetch_array($result))
          {
          echo "<tr>";
          echo "<td><a href=\"main.php?cmd=display&index=".$row['index']."\" target=main>".$row['index']."</a></td>";
          echo "<td>" . $row['uid']. "</td>";
          echo "</tr>";
          }
        echo "</table></center>";

        mysql_free_result($result);
        mysql_close($db);

}

function displayuser($database,$index) {
	 $db=mysql_connect($database['host'],$database['user'],$database['password']) or die("Could not connect");
        mysql_select_db($database['database']) or die('Could not select database');

	$sql="select * from ".$database['users_table']." where `index`=$index";
	$result=mysql_query($sql);
	$row = mysql_fetch_array($result);
	echo "Searching for user: ".$row['uid']."<br>";
	echo "<table>";
		echo "<tr><th>\</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th></tr>";
		echo "<tr><th>A</th><td>".$row['1']."<td>".$row['2']." </td><td>".$row['3']." </td><td>".$row['4']." </td><td>".$row['5']." </td></tr>";
		echo "<tr><th>B</th><td>".$row['6']."<td>".$row['7']." </td><td>".$row['8']." </td><td>".$row['9']." </td><td>".$row['10']." </td></tr>";
		echo "<tr><th>C</th><td>".$row['11']."<td>".$row['12']." </td><td>".$row['13']." </td><td>".$row['14']." </td><td>".$row['15']." </td></tr>";
		echo "<tr><th>D</th><td>".$row['16']."<td>".$row['17']." </td><td>".$row['18']." </td><td>".$row['19']." </td><td>".$row['20']." </td></tr>";
		echo "<tr><th>E</th><td>".$row['21']."<td>".$row['22']." </td><td>".$row['23']." </td><td>".$row['24']." </td><td>".$row['25']." </td></tr>";
		echo "<tr><th>F</th><td>".$row['26']."<td>".$row['27']." </td><td>".$row['28']." </td><td>".$row['29']." </td><td>".$row['30']." </td></tr>";
		echo "<tr><th>G</th><td>".$row['31']."<td>".$row['32']." </td><td>".$row['33']." </td><td>".$row['34']." </td><td>".$row['35']." </td></tr>";

        mysql_free_result($result);
        mysql_close($db);

}



function edituser ($database,$uid) {
	echo "Creating user: ".$uid;


	$a1 = rand(1111,9999);
	$a2 = rand(1111,9999);
	$a3 = rand(1111,9999);
	$a4 = rand(1111,9999);
	$a5 = rand(1111,9999);
	$b1 = rand(1111,9999);
	$b2 = rand(1111,9999);
	$b3 = rand(1111,9999);
	$b4 = rand(1111,9999);
	$b5 = rand(1111,9999);
	$c1 = rand(1111,9999);
	$c2 = rand(1111,9999);
	$c3 = rand(1111,9999);
	$c4 = rand(1111,9999);
	$c5 = rand(1111,9999);
	$d1 = rand(1111,9999);
	$d2 = rand(1111,9999);
	$d3 = rand(1111,9999);
	$d4 = rand(1111,9999);
	$d5 = rand(1111,9999);
	$e1 = rand(1111,9999);
	$e2 = rand(1111,9999);
	$e3 = rand(1111,9999);
	$e4 = rand(1111,9999);
	$e5 = rand(1111,9999);
	$f1 = rand(1111,9999);
	$f2 = rand(1111,9999);
	$f3 = rand(1111,9999);
	$f4 = rand(1111,9999);
	$f5 = rand(1111,9999);
	$g1 = rand(1111,9999);
	$g2 = rand(1111,9999);
	$g3 = rand(1111,9999);
	$g4 = rand(1111,9999);
	$g5 = rand(1111,9999);

	$db=mysql_connect($database['host'],$database['user'],$database['password']) or die("Could not connect");
        mysql_select_db($database['database']) or die('Could not select database');

	$sql="insert into users (`index`,`uid`,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`,`13`,`14`,`15`,`16`,`17`,`18`,`19`,`20`,`21`,`22`,`23`,`24`,`25`,`26`,`27`,`28`,`29`,`30`,`31`,`32`,`33`,`34`,`35`) values ".
//	$sql="insert into users (index,uid,a1) values ".
	$sql .= "(0,'$uid',$a1,$a2,$a3,$a4,$a5,$b1,$b2,$b3,$b4,$b5,$c1,$c2,$c3,$c4,$c5,$d1,$d2,$d3,$d4,$d5,$e1,$e2,$e3,$e4,$e5,$f1,$f2,$f3,$f4,$f5,$g1,$g2,$g3,$g4,$g5)";
//	$sql .= "(0,'$uid',$a1)";
//	echo "<br>".$sql."<br>"; 
	$result=mysql_query($sql) or die (mysql_error());
        mysql_free_result($result);
        mysql_close($db);
	echo "<br>Action completed successfully.";
}


function adduser () {
	echo "<center><form METHOD=GET action=main.php><input type=text label=\"User ID\" name=uid><input type=submit name=submit value=\"Add User\"><input type=hidden name=cmd value=edit></form></center>";	
}

switch ($_GET['cmd']) {
	case "browse": 
		browse($database);
		break;
	case "display":
		displayuser($database,$_GET['index']);
		break;
	case "edit":
		edituser($database,$_GET['uid']);
		break;
	case "add":
		adduser();
		break;
	default:
		echo "Please select an option on the left";
}

?>	




